package persistence;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Venta;

public class VentaDAOFileImplementation implements VentaDAO {

    private Map<String, Venta> ventaMap = new HashMap<String, Venta>();
    private String fileName;
    private static VentaDAOFileImplementation persistenceManager = null;

    private VentaDAOFileImplementation() {
    }

    public static VentaDAO getJDBCPersistenceManager() {
        if (persistenceManager == null) {
            persistenceManager = new VentaDAOFileImplementation();
        }
        return persistenceManager;
    }

    @Override
    public boolean setUp(String url, String driver, String user, String password) {
        this.fileName = url;
        try {
            File file = new File(fileName);
            if (!file.exists()) {
                return true;
            }
            InputStream inputStream = new FileInputStream(file);
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            int numberOfProducts = (Integer) objectInputStream.readObject();
            for (int i = 0; i < numberOfProducts; i++) {
                Venta venta = (Venta) objectInputStream.readObject();
                getVentaMap().put(venta.getIdentificador(), venta);
            }
            objectInputStream.close();
        } catch (Exception ex) {
            Logger.getLogger(ProductoDAOFileImplementation.class.getName()).log(Level.SEVERE,
                    "No se pudieron cargar los datos del archivo", ex);
            return false;
        }
        return true;
    }

    @Override
    public boolean disconnect() {
        try {
            OutputStream outputStream = new FileOutputStream(new File(fileName));
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(getVentaMap().size());
            for (Venta venta : getVentaMap().values()) {
                objectOutputStream.writeObject(venta);
            }
            objectOutputStream.close();
        } catch (IOException ex) {
            Logger.getLogger(ProductoDAOFileImplementation.class.getName()).log(Level.SEVERE,
                    "No se pudieron guardar los datos en el archivo", ex);
            return false;
        }
        return true;
    }

    @Override
    public synchronized boolean newVenta (Venta venta) {
        if (getVentaMap().containsKey(venta.getIdentificador())) {
            return false;
        } else {
            getVentaMap().put(venta.getIdentificador(), venta);
            return true;
        }
    }

    @Override
    public synchronized boolean deleteVenta(String name) {
        if (!ventaMap.containsKey(name)) {
            return false;
        } else {
            getVentaMap().remove(name);
            return true;
        }
    }

    @Override
    public synchronized boolean updateVenta(String name, Venta venta) {
        if (!ventaMap.containsKey(name)) {
            return false;
        } else {
            getVentaMap().put(venta.getIdentificador(), venta);
            return true;
        }
    }

    @Override
    public synchronized Venta readVenta(String name) {
        return getVentaMap().get(name);
    }

    /**
     * @return the productMap
     */
    @Override
    public Map<String, Venta> getVentaMap() {
        return ventaMap;
    }



}
